package com.chapter14.list_;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
 * @author 宋鑫宇
 * @version 1.0
 */
public class ListExercise02 {
    @SuppressWarnings("all")
    public static void main(String[] args) {
        List list = new ArrayList();
        list.add(new Book("西游记",100,"吴承恩"));
        list.add(new Book("红楼梦",110,"曹雪芹"));
        list.add(new Book("水浒传",90,"施耐庵"));
        for (Object o : list) {
            System.out.println(o);
        }
        System.out.println("=============");
        list.sort(new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                Book b1 = (Book) o1;
                Book b2 = (Book) o2;
                double tempNum = b1.getPrice()-b2.getPrice();
                if (tempNum>0){
                    return 1;
                }else if (tempNum<0) {
                    return -1;
                }
                return 0;
            }
        });
        for (Object o :list) {
            System.out.println(o);
        }

    }
}
class Book{
    private String name;
    private double price;
    private String zz;

    public Book(String name, double price, String zz) {
        this.name = name;
        this.price = price;
        this.zz = zz;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public String getZz() {
        return zz;
    }

    public void setZz(String zz) {
        this.zz = zz;
    }

    @Override
    public String toString() {
        return "Book{" +
                "名称'" + name + '\'' +
                ", 价格" + price +
                ", 作者'" + zz + '\'' +
                '}';
    }
}
